package com.modelo.dao.vehiculo;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.modelo.entity.Vehiculo;

@Repository
public class VehiculoDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(VehiculoDAO.class);
	 
	public static final String MATRICULA = "matricula";
	public static final String MARCA = "marca";
	public static final String MODELO = "modelo";
	public static final String OBSERVACIONES = "observaciones";
	public static final String ES_MARCADO_CE = "esMarcadoCe";
	public static final String SUP_VEL_ITV = "supVelItv";

	 
	 @Autowired  
	 public VehiculoDAO(SessionFactory sessionFactory) {  
	        super.setSessionFactory(sessionFactory);  
	 } 
	public List<Vehiculo> findByExample(Vehiculo instance) {
		log.debug("finding Vehiculo instance by example");
		try {
			List<Vehiculo> results = (List<Vehiculo>) getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Vehiculo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Vehiculo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Vehiculo> findByMatricula(Object matricula) {
		return findByProperty(MATRICULA, matricula);
	}

	public List<Vehiculo> findByMarca(Object marca) {
		return findByProperty(MARCA, marca);
	}

	public List<Vehiculo> findByModelo(Object modelo) {
		return findByProperty(MODELO, modelo);
	}

	public List<Vehiculo> findByObservaciones(Object observaciones) {
		return findByProperty(OBSERVACIONES, observaciones);
	}

	public List<Vehiculo> findByEsMarcadoCe(Object esMarcadoCe) {
		return findByProperty(ES_MARCADO_CE, esMarcadoCe);
	}

	public List<Vehiculo> findBySupVelItv(Object supVelItv) {
		return findByProperty(SUP_VEL_ITV, supVelItv);
	}

	public static VehiculoDAO getFromApplicationContext(ApplicationContext ctx) {
		return (VehiculoDAO) ctx.getBean("VehiculoDAO");
	}
}